//
// Created by Administrator on 2018-04-03.
//

#include <cstring>
#include "MyString.h"

MyString::MyString() {
    s_len = 0;
    s_p = new char[s_len + 1];
    strcpy(s_p, "");

}

MyString::MyString(char *p) {
    if (p == NULL) {
        s_len = 0;
        s_p = new char[s_len + 1];
        strcpy(s_p, "");
    } else {
        s_len = strlen(p);
        s_p = new char[s_len + 1];
        strcpy(s_p, p);
    }
}

MyString::MyString(const MyString &string) {
    s_len = string.s_len;
    s_p = new char[s_len + 1];
    strcpy(s_p, string.s_p);
}

MyString::~MyString() {
    if (s_p != NULL) {
        delete[] s_p;
        s_p = NULL;
        s_len = 0;
    }
}


MyString &MyString::operator=(const MyString &sp) {
    if (s_p != NULL) {
        delete[] s_p;
        s_len = 0;
    }
    s_len = sp.s_len;
    s_p = new char[s_len + 1];
    strcpy(s_p, sp.s_p);
    return *this;

}

MyString &MyString::operator=(const char *c) {
    if (s_p != NULL) {
        delete[] s_p;
        s_len = 0;
    }

    if (c == NULL) {
        s_len = 0;
        s_p = new char[s_len + 1];
        strcpy(s_p, "");
    } else {
        s_len = strlen(c);
        s_p = new char[s_len + 1];
        strcpy(s_p, c);
    }
    return *this;
}

bool MyString::operator==(const char *cp) {
    if (cp == NULL) {
        if (s_len == 0) {
            return true;
        } else {
            return false;
        }
    } else {
        if (s_len == strlen(cp)) {
            return !strcmp(s_p, cp);
        } else {
            return false;
        }
    }
}

bool MyString::operator==(const MyString &sp) {
    if (s_len != sp.s_len) {
        return false;
    }
    return !strcmp(s_p, sp.s_p);
}

bool MyString::operator!=(const char *cp) {
    if(cp == NULL){
        if(s_len == 0){
            return false;
        }else{
            return true;
        }
    }else{
        if(s_len == strlen(cp)){
            return strcmp(s_p , cp);
        }else{
            return true;
        }
    }
}

bool MyString::operator!=(const MyString &sp) {
    if (s_len == sp.s_len) {
        return strcmp(s_p , sp.s_p);
    } else {
        return true;
    }
}

ostream &operator<<(ostream &out, const MyString &sp) {
    out<<sp.s_p;
    return out;
}

istream &operator>>(istream &in, const MyString &sp) {
    in>>sp.s_p;
    return in;
}

